﻿using HandyControl.Controls;
using HandyControl.Data;
using IoTClient.Clients.PLC;
using IoTClient.Enums;
using QModels;
using QServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QControlCenter
{
	/// <summary>
	/// 三菱plc
	/// </summary>
	public class QMitsubishiPlc
	{

		private string ip { get; set; }
		private int port { get; set; }
		private MitsubishiVersion mitsubishiVersion { get; set; }

		private MitsubishiClient m_plc;

		/// <summary>
		/// MitsubishiClient client = new MitsubishiClient(MitsubishiVersion.Qna_3E, "127.0.0.1", 6000);
		/// </summary>
		/// <param name="ip"></param>
		/// <param name="port"></param>
		/// <param name="version"></param>
		public QMitsubishiPlc(string ip, int port, MitsubishiVersion version)
		{
			this.ip = ip;
			this.port = port;

			this.mitsubishiVersion = version;

			m_plc = new MitsubishiClient(mitsubishiVersion, ip, port);
		}

		public MitsubishiClient GetPlc()
		{
			
			if (m_plc == null) m_plc = new MitsubishiClient(mitsubishiVersion, ip, port);
			if (!m_plc.Connected) m_plc.Open();
			return m_plc;
		}



		#region 三菱plc读操作
		//温度探头1	07D0	读
		//温度探头2	07D1	读
		//温度探头3	07D2	读
		//室外温度探头	07D3	读
		//二氧化碳探头	07D4	读

		public int 温度探头1()
		{
			var plc = GetPlc();
			if (!plc.Connected)
			{
				Growl.ErrorGlobal("打开PLC失败");
				AppRuntime.Logger_Error("打开PLC失败");
				return default;
			}
			var res = plc.ReadInt32(QMitsubishiPlcAddress.温度探头1).Value;
			return res;

		}
		public int 温度探头2()
		{
			var plc = GetPlc();
			if (!plc.Connected)
			{
				Growl.ErrorGlobal("打开PLC失败");
				AppRuntime.Logger_Error("打开PLC失败");
				return default;
			}
			var res = plc.ReadInt32(QMitsubishiPlcAddress.温度探头2).Value;
			return res;

		}
		public int 温度探头3()
		{
			var plc = GetPlc();
			if (!plc.Connected)
			{
				Growl.ErrorGlobal("打开PLC失败");
				AppRuntime.Logger_Error("打开PLC失败");
				return default;
			}
			var res = plc.ReadInt32(QMitsubishiPlcAddress.温度探头3).Value;
			return res;

		}

		/// <summary>
		/// 获取室内平均温度
		/// </summary>
		/// <returns></returns>
		public (decimal temperature_in,decimal temperature_out, int OK) GetAvageTemperature()
		{
			var plc = GetPlc();
			if (!plc.Connected)
			{
				Growl.ErrorGlobal("打开PLC失败");
				AppRuntime.Logger_Error("打开PLC失败");
				return default;
			}
			int t_0 = 0;
			int t_1 = 0;
			int t_2 = 0;
			int OK = 0;
			var temperature_out = 0.0m;
			try
			{
				t_0 = plc.ReadInt32(QMitsubishiPlcAddress.温度探头1).Value;
				OK++;
			}
			catch (Exception ex)
			{
				AppRuntime.Logger_Error(ex);
			}
			try
			{
				t_1 = plc.ReadInt32(QMitsubishiPlcAddress.温度探头2).Value;
				OK++;
			}
			catch (Exception ex)
			{
				AppRuntime.Logger_Error(ex);
			}
			try
			{
				t_2 = plc.ReadInt32(QMitsubishiPlcAddress.温度探头3).Value;
				OK++;
			}
			catch (Exception ex)
			{
				AppRuntime.Logger_Error(ex);
			}
			var res = (t_0 + t_1 + t_2) / (0.0m + OK);
			try
			{
				temperature_out = plc.ReadInt32(QMitsubishiPlcAddress.室外温度探头).Value;
				OK++;
			}
			catch (Exception ex)
			{
				AppRuntime.Logger_Error(ex);
			}
			return (res,temperature_out ,OK);
		}

		public (decimal temperature_in, decimal temperature_out,decimal humidy,decimal co2,decimal o3) GetHomePageData()
		{
			var plc = GetPlc();
			if (!plc.Connected)
			{
				Growl.ErrorGlobal("打开PLC失败");
				AppRuntime.Logger_Error("打开PLC失败");
				return default;
			}
			int t_0 = 0;
			int t_1 = 0;
			int t_2 = 0;
			int OK = 0;
			int humidy = 0;
			int co2 = 0;
			int o3 = 0;
			var temperature_out = 0.0m;
			try
			{
				t_0 = plc.ReadInt32(QMitsubishiPlcAddress.温度探头1).Value;
				OK++;
			}
			catch (Exception ex)
			{
				AppRuntime.Logger_Error(ex);
			}
			try
			{
				t_1 = plc.ReadInt32(QMitsubishiPlcAddress.温度探头2).Value;
				OK++;
			}
			catch (Exception ex)
			{
				AppRuntime.Logger_Error(ex);
			}
			try
			{
				t_2 = plc.ReadInt32(QMitsubishiPlcAddress.温度探头3).Value;
				OK++;
			}
			catch (Exception ex)
			{
				AppRuntime.Logger_Error(ex);
			}
			var res = (t_0 + t_1 + t_2) / (0.0m + OK);
			try
			{
				temperature_out = plc.ReadInt32(QMitsubishiPlcAddress.室外温度探头).Value;
				OK++;
			}
			catch (Exception ex)
			{
				AppRuntime.Logger_Error (ex);
			}

			try
			{
				co2= plc.ReadInt32(QMitsubishiPlcAddress.二氧化碳探头).Value;
			}
			catch(Exception ex)
			{
				AppRuntime.Logger_Error(ex); 
			}
			try
			{
				
			}
			catch(Exception ex)
			{
				AppRuntime.Logger_Error(ex);
			}

			return (res, temperature_out, humidy, co2, o3);




		}



		public int 室外温度探头()
		{
			var plc = GetPlc();
			if (!plc.Connected)
			{
				Growl.ErrorGlobal("打开PLC失败");
				AppRuntime.Logger_Error("打开PLC失败");
				return default;
			}
			var res = plc.ReadInt32(QMitsubishiPlcAddress.室外温度探头).Value;
			return res;

		}
		public int 二氧化碳探头()
		{
			var plc = GetPlc();
			if (!plc.Connected)
			{
				Growl.ErrorGlobal("打开PLC失败");
				AppRuntime.Logger_Error("打开PLC失败");
				return default;
			}
			var res = plc.ReadInt32(QMitsubishiPlcAddress.二氧化碳探头).Value;
			return res;

		}





		public int 导流板下限位()
		{
			var plc = GetPlc();
			if (!plc.Connected)
			{
				Growl.ErrorGlobal("打开PLC失败");
				AppRuntime.Logger_Error("打开PLC失败");
				return default;
			}
			var res = plc.ReadInt32(QMitsubishiPlcAddress.导流板下限位).Value;
			return res;
		}
		public int 导流板上限位()
		{
			var plc = GetPlc();
			if (!plc.Connected)
			{
				Growl.ErrorGlobal("打开PLC失败");
				AppRuntime.Logger_Error("打开PLC失败");
				return default;
			}
			var res = plc.ReadInt32(QMitsubishiPlcAddress.导流板上限位).Value;
			return res;
		}

		public HuanKongModel GetHuanKong()
		{
		
			var res=new HuanKongModel();
			var plc = GetPlc();
			if (!plc.Connected)
			{
				Growl.ErrorGlobal("打开PLC失败");
				AppRuntime.Logger_Error("打开PLC失败");
				return default;
			}
			try
			{
				var t_0=plc.ReadInt32(QMitsubishiPlcAddress.水泵).Value;
				res.ShuiLian_OK++;
			}
			catch(Exception ex)
			{
				res.ShuiLian_Error++;
				AppRuntime.Logger_Error(ex);
			}

			try
			{
				var t_0 = plc.ReadInt32(QMitsubishiPlcAddress.导流板).Value;
				res.DaoLiuBan_OK++;
			}
			catch(Exception ex)
			{
				res.DaoLiuBan_Error++;
				AppRuntime.Logger_Error(ex);
			}
			try
			{
				var t_0 = plc.ReadInt32(QMitsubishiPlcAddress.电动百叶窗).Value;
				res.BaiYeChuang_OK++;
			}
			catch(Exception ex)
			{
				res.BaiYeChuang_Error++;
				AppRuntime.Logger_Error(ex);
			}

			try
			{
				var t_0 = plc.ReadInt32(QMitsubishiPlcAddress.新风开关).Value;
				res.XinFeng_OK++;
			}
			catch(Exception ex)
			{
				res.XinFeng_Error++;
				AppRuntime.Logger_Error(ex);
			}

			for(int i = 5; i < 10; i++)
			{
				var address = $"07D{i}";
				try
				{
					var t_0 = plc.ReadInt32(address).Value;
					res.DaFengJiZu_OK++;
				}
				catch(Exception ex)
				{
					res.DaFengJiZu_Error++;
					AppRuntime.Logger_Error(ex);
				}
			}

			for (int i = 10; i < 16; i++)
			{
				var address = $"07D{i}";
				try
				{
					var t_0 = plc.ReadInt32(address).Value;
					res.CePaiFeng_OK++;
				}
				catch (Exception ex)
				{
					res.CePaiFeng_Error++;
					AppRuntime.Logger_Error(ex);
				}
			}


			return res;
		}

		#endregion


		#region 三菱plc写操作
		/// <summary>
		/// 模式控制
		/// 写入1对应手动模式写入
		/// 2对应冬季模式写入
		/// 3对应春秋季模式写入
		/// 4对应夏季模式
		/// </summary>
		/// <param name="model">写入1对应手动模式写入2对应冬季模式写入3对应春秋季模式写入4对应夏季模式</param>
		public void ModeControl(int model)
		{
			var plc = GetPlc();
			plc.Write(QMitsubishiPlcAddress.模式控制, model);
			AppRuntime.Logger_Info("设置模式控制");
		}
		public void 大风机1组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.大风机1组开关, value);
			AppRuntime.Logger_Info("设置大风机1组开关");
		}
		public void 大风机2组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.大风机2组开关, value);
			AppRuntime.Logger_Info("设置大风机2组开关");
		}
		public void 大风机3组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.大风机3组开关, value);
			AppRuntime.Logger_Info("设置大风机3组开关");
		}
		public void 大风机4组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.大风机4组开关, value);
			AppRuntime.Logger_Info("设置大风机4组开关");
		}
		public void 大风机5组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.大风机5组开关, value);
			AppRuntime.Logger_Info("设置大风机5组开关");
		}


		public void 侧排风1组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.侧排风1组开关, value);
			AppRuntime.Logger_Info("设置侧排风1组开关");
		}
		public void 侧排风2组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.侧排风2组开关, value);
			AppRuntime.Logger_Info("设置侧排风2组开关");
		}
		public void 侧排风3组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.侧排风3组开关, value);
			AppRuntime.Logger_Info("设置侧排风3组开关");
		}
		public void 侧排风4组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.侧排风4组开关, value);
			AppRuntime.Logger_Info("设置侧排风4组开关");
		}
		public void 侧排风5组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.侧排风5组开关, value);
			AppRuntime.Logger_Info("设置侧排风5组开关");
		}
		public void 侧排风6组开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.侧排风6组开关, value);
			AppRuntime.Logger_Info("设置侧排风6组开关");
		}
		public void 新风开关(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.新风开关, value);
			AppRuntime.Logger_Info("新风开关");
		}

		/// <summary>
		/// 写入1开启50%，写入2开100%，写入0关闭
		/// </summary>
		/// <param name="open"></param>
		public void 电动百叶窗(int value)
		{
			var plc = GetPlc();

			plc.Write(QMitsubishiPlcAddress.电动百叶窗, value);
			AppRuntime.Logger_Info("电动百叶窗");
		}
		public void 水泵(bool open)
		{
			var plc = GetPlc();
			var value = open ? 1 : 0;
			plc.Write(QMitsubishiPlcAddress.水泵, value);
			AppRuntime.Logger_Info("水泵");
		}

		/// <summary>
		/// 写入1开启30%，写入2开600%，写入3开启100%，写入0关闭
		/// </summary>
		/// <param name="open"></param>
		public void 导流板(int value)
		{
			var plc = GetPlc();

			plc.Write(QMitsubishiPlcAddress.导流板, value);
			AppRuntime.Logger_Info("导流板");
		}


		/// <summary>
		/// 写入0是PLC控制，写入1是PC控制
		/// </summary>
		/// <param name="open"></param>
		public void PC与PLC切换(int value)
		{
			var plc = GetPlc();

			plc.Write(QMitsubishiPlcAddress.PC与PLC切换, value);
			AppRuntime.Logger_Info("PC与PLC切换");
		}




		public void UpdateDevicePage(DosingEquipment info)
		{
			var plc = GetPlc();
			try
			{
				var dafengjizu = new int[] 
				{ 
					info.DaFengJi_open_1?1:0,
					info.DaFengJi_open_2?1:0,
					info.DaFengJi_open_3?1:0,
					info.DaFengJi_open_4?1:0,
					info.DaFengJi_open_5?1:0,
				};
				var cepaifeng = new int[]
				{
					info.CePaiFeng_open_1?1:0,
					info.CePaiFeng_open_2?1:0,
					info.CePaiFeng_open_3?1:0,
					info.CePaiFeng_open_4?1:0,
					info.CePaiFeng_open_5?1:0,
					info.CePaiFeng_open_6?1:0,

				};
				for (int i = 0; i < 5; i++)
				{
					var tt = i + 5;
					var address = $"07D{tt}";
					plc.Write(address, dafengjizu[i]);
					
				}

				for (int i = 0; i < 6; i++)
				{
					var tt = i + 10;
					var address = $"07D{tt}";
					plc.Write(address, cepaifeng[i]);
					
				}

				plc.Write(QMitsubishiPlcAddress.新风开关, info.XinFengJi_open_1 ? 1 : 0);
				plc.Write(QMitsubishiPlcAddress.电动百叶窗, info.BaiYeChuang_open_1 );
				plc.Write(QMitsubishiPlcAddress.水泵, info.ShuiBeng_open_1);
				plc.Write(QMitsubishiPlcAddress.导流板 , info.DaoLiuBan_open_1);


			}
			catch(Exception ex)
			{
				AppRuntime.Logger_Error(ex);
			}

		}





		#endregion



	}
}
